#include "ExplorBotDialog.h"
#include "ExplorBotLibrary.h"
#include "ExplorBot.h"
#include "DIVA_VM.h"
#include "xVMRelationStruct.h"
#include <QLineEdit>
#include <QLabel>
#include <string>
#include <QGridLayout>
#include <QCheckBox>
#include <QFile>
#include <QComboBox>

#define MAX 500


using namespace std;

ExplorBotDialog::ExplorBotDialog(QWidget *parent, Qt::WFlags flags, DIVA_VM* vm,ExplorBot* eb)
{
	ui.setupUi(this);
	selectedVM = vm;
	_save = false;
	if (eb == NULL)
	{
		initDialog(selectedVM );
	}
	else
	{
		nweDialog(eb);
	}
	setStyle();
}


void ExplorBotDialog::nweDialog(ExplorBot* eb){
	assert(eb != NULL);
	initDialog(NULL);
	botNameArea->setText(QString::fromStdString(eb->getName()));
	varnmaeLine->setText(QString::fromStdString(eb->getVarname()));
	maxlevelLine->setText(QString::fromStdString(eb->getMaxLevelString()));
	maxNumLine->setText(QString::fromStdString(eb->getMaxNumString()));
	if (eb->getSelectRef().size()==0)
	{
		allc->setTristate(true);
		notallc->setTristate(false);;
	}
	else
	{
		clickAllRefStateChange(false);
		allc->setTristate(false);
		notallc->setTristate(true);
		clearRefParameterLayout();
		for(int i =0;i<eb->getSelectRef().size();i++){
			addNewRefLayout("",eb->getSelectRef().at(i));
		}
	}

}

void ExplorBotDialog::setStyle()
{
	QFile* cssFile = new QFile(":/resource/style.css");
	assert(cssFile);
	cssFile->open(QFile::ReadOnly);
	this->setStyleSheet(cssFile->readAll());
	cssFile->close();
	delete cssFile;

	QPalette palette;
	palette.setColor(QPalette::Background, QColor("#333333"));
	//palette.setBrush(QPalette::Background, QBrush(QPixmap(":/background.png")));
	ui.centralwidget->setPalette(palette);
}

void ExplorBotDialog::setupNewExplorBot()
{
	int maxl,maxn;
	if (maxlevelLine->text()=="(no limit)")
	{
		maxl = MAX;
	} 
	else
	{
		maxl = maxlevelLine->text().toInt();
		maxn = maxNumLine->text().toInt();
	}
	if (maxNumLine->text()=="(no limit)")
	{
		maxn = MAX;
	} 
	else
	{
		maxn = maxNumLine->text().toInt();
	}




	for (int i=0;i<_typeRefList.size();++i)
	{
			_selectRefList.push_back(_typeRefList.at(i)->text().toUtf8().constData());
	}


	string botName = botNameArea->text().toUtf8().constData();
	ExplorBot* eb;
	if (selectedVM == NULL)
	{
     	string varname = varnmaeLine->text().toUtf8().constData();
		eb = new ExplorBot(botName,varname,maxl,maxn,_selectRefList,true);
		eb->setExAllref(allc->checkState());
		eb->setDialog(this);
		eb->setSaved(_save);
	} 
	else
	{
		//if(ui.autobox->isChecked())
		eb = new ExplorBot(botName,selectedVM,maxl,maxn,_selectRefList);
		eb->setExAllref(allc->checkState());
		eb->setDialog(this);
		eb->setSaved(false);
	}
	eb->setMaxLevelString(maxlevelLine->text().toUtf8().constData());
	eb->setMaxNumString(maxNumLine->text().toUtf8().constData());
	ExplorBotLibrary::getInstance()->appendExplorBot(eb);
	this->close();

}

void ExplorBotDialog::initDialog(DIVA_VM* vm)
{
	ui.okbutton->setIcon(QIcon("images/check.png"));
	ui.CancelButton->setIcon(QIcon("images/Cancel-icon.png"));
	ui.okbutton->setStyleSheet("color: #C0C0C0;");
	ui.CancelButton->setStyleSheet("color: #C0C0C0;");
	ui.saveapplyButton->setIcon(QIcon("images/save.png"));
	ui.saveapplyButton->setStyleSheet("color: #C0C0C0;");

	QHBoxLayout *parameterLayout = new QHBoxLayout();
	QLabel *label = new QLabel();
	
	QHBoxLayout *nameLayout = new QHBoxLayout();
	namelabel = new QLabel();
	namelabel->setText("Bot Name");
	namelabel->setStyleSheet("qproperty-alignment: 'AlignBottom | AlignLeft';");
	botNameArea = new QLineEdit("(Option)");
	botNameArea->setMaximumWidth(100);
	botNameArea->setAlignment(Qt::AlignHCenter);
	nameLayout->addWidget(namelabel);
	nameLayout->addWidget(botNameArea);
	ui.verticalLayout->addLayout(nameLayout);
	
	if (vm == NULL)
	{

		label->setAlignment(Qt::AlignLeft);
		label->setText("VM's varname");
		QCheckBox* applyall = new QCheckBox("Explore All VMs");
		parameterLayout->addWidget(label);
		varnmaeLine = new QLineEdit;
		varnmaeLine->setMaximumWidth(100);
		varnmaeLine->setAlignment(Qt::AlignHCenter);
		parameterLayout->addWidget(varnmaeLine);
		parameterLayout->addWidget(applyall);
		connect(applyall, SIGNAL(toggled(bool)), this, SLOT(clickExploreAllChange(bool)));
	} 
	else
	{
		label->setAlignment(Qt::AlignHCenter);
		label->setText("You have select a VM as a start for the explore bot");
		parameterLayout->addWidget(label);
		QPushButton* bt = new QPushButton(QString::fromStdString(vm->get_vm_name()));
		bt->setStyleSheet("color: #C0C0C0;");
		bt->setFlat(true);
		parameterLayout->addWidget(bt);
	}
	ui.verticalLayout->addLayout(parameterLayout);

	QVBoxLayout* selqbox= new QVBoxLayout();
	QHBoxLayout* chl = new QHBoxLayout();
	QLabel* ch = new QLabel("Targets to Explore Bot");
	chl->addWidget(ch);
	QPushButton* bt = new QPushButton;
	bt->setFlat(true);
	chl->addWidget(bt);
	selqbox->addLayout(chl);

	QHBoxLayout* selectall = new QHBoxLayout();
	allc = new QCheckBox("Any Ref. VM");
	allc->setCheckState(Qt::Checked);
	QPushButton* bt2 = new QPushButton;
	bt2->setFlat(true);
	selectall->addWidget(allc);
	selectall->addWidget(bt2);
		
	notallc = new QCheckBox("Select Ref. VMs");
	QPushButton* bt3 = new QPushButton;
	bt3->setFlat(true);
	selectall->addWidget(notallc);
	selectall->addWidget(bt3);
	selqbox->addLayout(selectall);
	connect(allc, SIGNAL(toggled(bool)), this, SLOT(clickAllRefStateChange(bool)));
	connect(notallc, SIGNAL(toggled(bool)), this, SLOT(clickSelectRefStateChange(bool)));
	ui.verticalLayout->addLayout(selqbox);


	_refsLayout = new QVBoxLayout;
	ui.verticalLayout->addLayout(_refsLayout);

	/************************************************************************/
	/* Setup Max level & Max Vms                                            */
	/************************************************************************/	

	parameterLayout = new QHBoxLayout();
	label = new QLabel();
	label->setAlignment(Qt::AlignHCenter);
	maxlevelLine = new QLineEdit;
	maxlevelLine->setMaximumWidth(100);
	maxlevelLine->setAlignment(Qt::AlignHCenter);
	label->setText("The maximum level to explore");
	parameterLayout->addWidget(label);
	parameterLayout->addWidget(maxlevelLine);
	maxlevelLine->setText("(no limit)");
	ui.verticalLayout->addLayout(parameterLayout);

	parameterLayout = new QHBoxLayout();
	label = new QLabel();
	label->setAlignment(Qt::AlignHCenter);
	maxNumLine = new QLineEdit;
	maxNumLine->setMaximumWidth(100);
	maxNumLine->setAlignment(Qt::AlignHCenter);
	label->setText("The maximum number of VMs to explore");
	parameterLayout->addWidget(label);
	parameterLayout->addWidget(maxNumLine);
	maxNumLine->setText("(no limit)");
	ui.verticalLayout->addLayout(parameterLayout);
	///////////////////////////////////////////////////////////////////////

	connect(ui.okbutton, SIGNAL(clicked()), this, SLOT(setupNewExplorBot()));
	connect(ui.CancelButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(ui.actionSave, SIGNAL(clicked()), this, SLOT(save()));
	connect(ui.saveapplyButton, SIGNAL(clicked()), this, SLOT(save()));
}

void ExplorBotDialog::unFocus()
{
	addNewRefLayout("","");
}

void ExplorBotDialog::addNewRefLayout(std::string type,std::string target){
	QHBoxLayout* tmp = new QHBoxLayout();
	QCheckBox* useBox = new QCheckBox;
	useBox->setCheckState(Qt::Checked);
	QComboBox* selType = new QComboBox();
	selType->addItem("Ref. VM Varname");
	selType->addItem("VM Varname");
	QLineEdit* refa = new QLineEdit;
	refa->setMaximumWidth(100);
	refa->setText(QString::fromStdString(target));
	QPushButton* more = new QPushButton("More");
	more->setStyleSheet("color: #C0C0C0;");
	more->setFlat(true);
	tmp->addWidget(useBox);
	tmp->addWidget(selType);
	tmp->addWidget(refa);
	tmp->addWidget(more);
	_typeRefList.push_back(refa);
	_refsLayout->addLayout(tmp);
	connect(refa,SIGNAL(returnPressed ()),this,SLOT(unFocus()));
	connect(more,SIGNAL(clicked ()),this,SLOT(unFocus()));
}

void ExplorBotDialog::save(){
	_save = true;
	setupNewExplorBot();
}

void ExplorBotDialog::clickAllRef(){
	clearRefParameterLayout();
}

void ExplorBotDialog::clickSelectRef(){
	clearRefParameterLayout();
	QHBoxLayout* title = new QHBoxLayout();
	QLabel* used = new QLabel("Use");
	QLabel* type = new QLabel("Type");
	QLabel* target = new QLabel("Target");
	title->addWidget(used);
	title->addWidget(type);
	title->addWidget(target);
	QPushButton* bt = new QPushButton;
	bt->setFlat(true);
	title->addWidget(bt);
	_refsLayout->addLayout(title);

	//////////////////////////////////////////////////////////////////////////
	addNewRefLayout("","");
}

void ExplorBotDialog::clickAllRefStateChange(bool i){
	if (i)
	{
		clickAllRef();
		notallc->setCheckState(Qt::Unchecked);
	}
	else
	{
		clickSelectRef();
		//allc->setCheckState(Qt::Checked);
	}
}

void ExplorBotDialog::clickSelectRefStateChange(bool i){
	if (i)
	{
		clickSelectRef();
		allc->setCheckState(Qt::Unchecked);
	}
	else
	{
		clickAllRef();
		//notallc->setCheckState(Qt::Checked);
	}
}

void ExplorBotDialog::clearRefParameterLayout(){
	QLayoutItem* item;
	while ( ( item = _refsLayout->takeAt( 0 ) ) != NULL )
	{
		QHBoxLayout* item2 = dynamic_cast<QHBoxLayout*>(item);
		if (item2)
		{
			QLayoutItem* item3;
			while ( ( item3 = item2->takeAt( 0 ) ) != NULL )
			{
				delete item3->widget();
				delete item3;
			}
		}
		delete item;
	}
	_typeRefList.clear();
}

void ExplorBotDialog::clickExploreAllChange(bool i){
	clickExploreAll(i);
}
void ExplorBotDialog::clickExploreAll(bool i){
	if (i)
	{
		varnmaeLine->setText("[ALL]");
	}
	else
	{
		varnmaeLine->setText("");
	}
	varnmaeLine->setReadOnly(i);
}